package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javafx.application.Application;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void deleteByPrimaryKey(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Permission record) {
        permissionMapper.insert(record);
    }

    @Override
    public Permission selectByPrimaryKey(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> selectAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public void updateByPrimaryKey(Permission record) {
        permissionMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> permissions = permissionMapper.selectForList(qo);
        return new PageInfo<>(permissions);
    }

    @Autowired
    private ApplicationContext ctx;

    @Override
    public void reload() {
        //查询全部权限表达式
        List<String> expressions = permissionMapper.selectExpressions();
        //从spring容器中获取所有的controller
        Map<String, Object> beans = ctx.getBeansWithAnnotation(Controller.class);
        //拿map中的value
        Collection<Object> values = beans.values();
        //遍历循环获取每一个controller
        for (Object controller : values) {
            //获取controller字节码对象
            Class controllerClass = controller.getClass();
            //获取所有方法
            Method[] methods = controllerClass.getDeclaredMethods();
            for (Method method : methods) {
                RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
                //如果有贴注解,封装成权限对象,并插入数据库
                if (annotation!=null) {
                    //获取权限表达式
                    String expression = annotation.value()[0];
                    if(!expressions.contains(expression)){//如果集合中不包含才封装成对象,插入数据库
                        Permission permission = new Permission();
                        permission.setName(annotation.value()[1]);
                        permission.setExpression(annotation.value()[0]);
                        permissionMapper.insert(permission);

                    }


                }
            }
            //判断是否有贴注解
        }

    }
}
